#!/bin/bash

# API测试脚本
echo "=== 后端API测试脚本 ==="

# 设置API基础URL
BASE_URL="http://localhost:8080/api"

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# 检查服务是否运行
check_service() {
    echo -e "${YELLOW}检查服务状态...${NC}"
    if curl -s "$BASE_URL/health" > /dev/null; then
        echo -e "${GREEN}✓ 服务正在运行${NC}"
        return 0
    else
        echo -e "${RED}✗ 服务未运行，请先启动服务${NC}"
        return 1
    fi
}

# 测试用户注册
test_user_register() {
    echo -e "\n${YELLOW}测试用户注册...${NC}"
    response=$(curl -s -w "\n%{http_code}" -X POST "$BASE_URL/frontend/register" \
        -H "Content-Type: application/json" \
        -d '{
            "username": "testuser123",
            "email": "testuser123@example.com",
            "password": "password123",
            "nickname": "测试用户123"
        }')
    
    http_code=$(echo "$response" | tail -n1)
    body=$(echo "$response" | head -n -1)
    
    if [ "$http_code" = "200" ]; then
        echo -e "${GREEN}✓ 用户注册成功${NC}"
        echo "响应: $body"
    else
        echo -e "${RED}✗ 用户注册失败 (HTTP $http_code)${NC}"
        echo "响应: $body"
    fi
}

# 测试用户登录
test_user_login() {
    echo -e "\n${YELLOW}测试用户登录...${NC}"
    response=$(curl -s -w "\n%{http_code}" -X POST "$BASE_URL/frontend/login" \
        -H "Content-Type: application/json" \
        -d '{
            "username": "testuser",
            "password": "admin123"
        }')
    
    http_code=$(echo "$response" | tail -n1)
    body=$(echo "$response" | head -n -1)
    
    if [ "$http_code" = "200" ]; then
        echo -e "${GREEN}✓ 用户登录成功${NC}"
        echo "响应: $body"
        # 提取token
        USER_TOKEN=$(echo "$body" | grep -o '"token":"[^"]*' | cut -d'"' -f4)
        echo "Token: $USER_TOKEN"
    else
        echo -e "${RED}✗ 用户登录失败 (HTTP $http_code)${NC}"
        echo "响应: $body"
    fi
}

# 测试获取用户信息
test_user_profile() {
    if [ -z "$USER_TOKEN" ]; then
        echo -e "${RED}✗ 无用户Token，跳过用户信息测试${NC}"
        return
    fi
    
    echo -e "\n${YELLOW}测试获取用户信息...${NC}"
    response=$(curl -s -w "\n%{http_code}" -X GET "$BASE_URL/frontend/profile" \
        -H "Authorization: Bearer $USER_TOKEN")
    
    http_code=$(echo "$response" | tail -n1)
    body=$(echo "$response" | head -n -1)
    
    if [ "$http_code" = "200" ]; then
        echo -e "${GREEN}✓ 获取用户信息成功${NC}"
        echo "响应: $body"
    else
        echo -e "${RED}✗ 获取用户信息失败 (HTTP $http_code)${NC}"
        echo "响应: $body"
    fi
}

# 测试管理员登录
test_admin_login() {
    echo -e "\n${YELLOW}测试管理员登录...${NC}"
    response=$(curl -s -w "\n%{http_code}" -X POST "$BASE_URL/admin/login" \
        -H "Content-Type: application/json" \
        -d '{
            "username": "admin",
            "password": "admin123"
        }')
    
    http_code=$(echo "$response" | tail -n1)
    body=$(echo "$response" | head -n -1)
    
    if [ "$http_code" = "200" ]; then
        echo -e "${GREEN}✓ 管理员登录成功${NC}"
        echo "响应: $body"
        # 提取token
        ADMIN_TOKEN=$(echo "$body" | grep -o '"token":"[^"]*' | cut -d'"' -f4)
        echo "Token: $ADMIN_TOKEN"
    else
        echo -e "${RED}✗ 管理员登录失败 (HTTP $http_code)${NC}"
        echo "响应: $body"
    fi
}

# 测试获取用户列表
test_admin_user_list() {
    if [ -z "$ADMIN_TOKEN" ]; then
        echo -e "${RED}✗ 无管理员Token，跳过用户列表测试${NC}"
        return
    fi
    
    echo -e "\n${YELLOW}测试获取用户列表...${NC}"
    response=$(curl -s -w "\n%{http_code}" -X GET "$BASE_URL/admin/users/" \
        -H "Authorization: Bearer $ADMIN_TOKEN")
    
    http_code=$(echo "$response" | tail -n1)
    body=$(echo "$response" | head -n -1)
    
    if [ "$http_code" = "200" ]; then
        echo -e "${GREEN}✓ 获取用户列表成功${NC}"
        echo "响应: $body"
    else
        echo -e "${RED}✗ 获取用户列表失败 (HTTP $http_code)${NC}"
        echo "响应: $body"
    fi
}

# 主测试流程
main() {
    echo "开始API测试..."
    
    # 检查服务状态
    if ! check_service; then
        echo -e "${RED}请先启动后端服务: ./bin/backend-api${NC}"
        exit 1
    fi
    
    # 执行测试
    test_user_register
    test_user_login
    test_user_profile
    test_admin_login
    test_admin_user_list
    
    echo -e "\n${GREEN}=== API测试完成 ===${NC}"
}

# 运行主函数
main